package com.light.mytext.mycustomview

import android.graphics.BitmapFactory
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.light.mytext.R
import com.light.mytext.utiles.LoadUtils
import com.light.mytext.utiles.ToolUtils
import com.makeramen.roundedimageview.RoundedImageView
import kotlinx.android.synthetic.main.activity_pic.*
import net.idik.lib.slimadapter.SlimAdapter

class PicActivity : AppCompatActivity() {
    var drawable = arrayOf(R.mipmap.aa, R.mipmap.stars)
    var mAdapter: SlimAdapter? = null
    var mData = ArrayList<Int>()
    var strimgs = arrayOf(R.drawable.pic1, R.drawable.pic2)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pic)
        init()
    }

    fun init() {
        getData()
        LoadUtils.loading(this, null, recycle_pic, true) {}
        imv_touchview.setImageResource(R.drawable.pic1)
//        imv_touchview.setBackgroundDrawable(resources.getDrawable(R.mipmap.pic1))
//        val options = BitmapFactory.Options()
//        /**
//         * 最关键在此，把options.inJustDecodeBounds = true;
//         * 这里再decodeFile()，返回的bitmap为空，但此时调用options.outHeight时，已经包含了图片的高了
//         */
//        options.inJustDecodeBounds = false
////        var bitmap = ToolUtils.getBitmap(this, R.drawable.pic1)
//        var bitmap = BitmapFactory.decodeResource(resources, R.drawable.pic1,options)
        mAdapter =
            SlimAdapter.create().register<Int>(R.layout.item_customview_pic) { data, injector ->
                injector.with<RoundedImageView>(R.id.imv) {
                    Glide.with(this)
                        .load(data)
                        .apply(
                            RequestOptions()
                                .placeholder(data)
                                .centerCrop()
                                .error(data)
                                .dontAnimate()
                        )
                        .into(it)
                }
                injector.clicked(R.id.imv) {
                    imv_touchview.setImageResource(data)
                    /*Glide.with(this)
                        .load(data)
                        .apply(
                            RequestOptions()
                                .placeholder(data)
                                .centerCrop()
                                .error(data)
                                .dontAnimate()
                        )
                        .into(imv_touchview)*/
                }


            }.attachTo(recycle_pic)
        mAdapter!!.updateData(mData).notifyDataSetChanged()
    }

    fun getData() {
        mData.clear()
        strimgs.forEach {
            mData.add(it)
        }
    }
}